package com.yuan.jeinksgitlab;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yuaneg 2019/9/5
 */
public class GitLabCiCreateWebHook {

    static String hookUrl = "http://192.168.2.62:8099/project/point-dev-group/";
    static String getProjectInfo = "http://192.168.2.23/api/v4/projects?private_token=P4ErWyM-yCZxuZKCAKvD&&search=";
    static String createHook1 = "http://192.168.2.23/api/v4/projects/";
    static String createHook2 = "/hooks?private_token=P4ErWyM-yCZxuZKCAKvD";

    static String createBranch1 = "http://192.168.2.23/api/v4/projects/";
    static String createBranch2 = "/repository/branches?private_token=P4ErWyM-yCZxuZKCAKvD";

    public static void main(String[] args) {
        String project = "[{\"projectname\":\"cms-admin-item-task\",\"url\":\"http://192.168.2.23/member-service/cms-admin-item-task.git\"}, {\"projectname\":\"cms-admin-sys-basic-api\",\"url\":\"http://192.168.2.23/member-service/cms-admin-sys-basic-api.git\"}, {\"projectname\":\"cms-admin-web\",\"url\":\"http://192.168.2.23/member-service/cms-admin-web.git\"}, {\"projectname\":\"customer-cart-api\",\"url\":\"http://192.168.2.23/member-service/customer-cart-api.git\"}, {\"projectname\":\"customer-cart-basic-api\",\"url\":\"http://192.168.2.23/member-service/customer-cart-basic-api.git\"}, {\"projectname\":\"customer-item-api\",\"url\":\"http://192.168.2.23/member-service/customer-item-api.git\"}, {\"projectname\":\"customer-item-basic-api\",\"url\":\"http://192.168.2.23/member-service/customer-item-basic-api.git\"}, {\"projectname\":\"customer-order-api\",\"url\":\"http://192.168.2.23/member-service/customer-order-api.git\"}, {\"projectname\":\"customer-order-basic-api\",\"url\":\"http://192.168.2.23/member-service/customer-order-basic-api.git\"}, {\"projectname\":\"customer-sale-activity-basic-api\",\"url\":\"http://192.168.2.23/member-service/customer-sale-activity-basic-api.git\"}, {\"projectname\":\"jhd-activity-api\",\"url\":\"http://192.168.2.23/member-service/jhd-activity-api.git\"}, {\"projectname\":\"jhd-address-master-api\",\"url\":\"http://192.168.2.23/member-service/jhd-address-master-api.git\"}, {\"projectname\":\"jhd-area-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-area-api.git\"}, {\"projectname\":\"jhd-channel-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-channel-api.git\"}, {\"projectname\":\"jhd-client-auth-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-client-auth-api.git\"}, {\"projectname\":\"jhd-crm-mini-portal-api\",\"url\":\"http://192.168.2.23/member-service/jhd-crm-mini-portal-api.git\"}, {\"projectname\":\"jhd-customer-api\",\"url\":\"http://192.168.2.23/member-service/jhd-customer-api.git\"}, {\"projectname\":\"jhd-customer-crm-api\",\"url\":\"http://192.168.2.23/member-service/jhd-customer-crm-api.git\"}, {\"projectname\":\"jhd-customer-crm-basic-api\",\"url\":\"http://192.168.2.23/member-service/jhd-customer-crm-basic-api.git\"}, {\"projectname\":\"jhd-customer-crm-service-api\",\"url\":\"http://192.168.2.23/member-service/jhd-customer-crm-service-api.git\"}, {\"projectname\":\"jhd-gateway-api\",\"url\":\"http://192.168.2.23/member-service/jhd-gateway-api.git\"}, {\"projectname\":\"jhd-inventory-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-inventory-api.git\"}, {\"projectname\":\"jhd-item-batch-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-item-batch-api.git\"}, {\"projectname\":\"jhd-member-api\",\"url\":\"http://192.168.2.23/member-service/jhd-member-api.git\"}, {\"projectname\":\"jhd-member-sync\",\"url\":\"http://192.168.2.23/member-service/jhd-member-sync.git\"}, {\"projectname\":\"jhd-merchant-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-api.git\"}, {\"projectname\":\"jhd-merchant-basic-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-basic-api.git\"}, {\"projectname\":\"jhd-merchant-batch-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-batch-api.git\"}, {\"projectname\":\"jhd-merchant-cart-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-cart-api.git\"}, {\"projectname\":\"jhd-merchant-item-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-item-api.git\"}, {\"projectname\":\"jhd-merchant-item-base-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-item-base-api.git\"}, {\"projectname\":\"jhd-merchant-order-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-order-api.git\"}, {\"projectname\":\"jhd-merchant-order-basic-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-merchant-order-basic-api.git\"}, {\"projectname\":\"jhd-message-api\",\"url\":\"http://192.168.2.23/member-service/jhd-message-api.git\"}, {\"projectname\":\"jhd-message-notify\",\"url\":\"http://192.168.2.23/member-service/jhd-message-notify.git\"}, {\"projectname\":\"jhd-netty-server-api\",\"url\":\"http://192.168.2.23/member-service/jhd-netty-server-api.git\"}, {\"projectname\":\"jhd-order-api\",\"url\":\"http://192.168.2.23/member-service/jhd-order-api.git\"}, {\"projectname\":\"jhd-promotion-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-promotion-api.git\"}, {\"projectname\":\"jhd-promotion-basic-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-promotion-basic-api.git\"}, {\"projectname\":\"jhd-promotion-batch-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-promotion-batch-api.git\"}, {\"projectname\":\"jhd-qrcode-generator-api\",\"url\":\"http://192.168.2.23/member-service/jhd-qrcode-generator-api.git\"}, {\"projectname\":\"jhd-resource-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-resource-api.git\"}, {\"projectname\":\"jhd-sales-cart-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-sales-cart-api.git\"}, {\"projectname\":\"jhd-sales-cart-basic-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-sales-cart-basic-api.git\"}, {\"projectname\":\"jhd-sales-reserve-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-sales-reserve-api.git\"}, {\"projectname\":\"jhd-sales-reserve-basic-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-sales-reserve-basic-api.git\"}, {\"projectname\":\"jhd-search-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-search-api.git\"}, {\"projectname\":\"jhd-sequence-api\",\"url\":\"http://192.168.2.23/member-service/jhd-sequence-api.git\"}, {\"projectname\":\"jhd-shipment-gateway-api\",\"url\":\"http://192.168.2.23/shipment-service/jhd-shipment-gateway-api.git\"}, {\"projectname\":\"jhd-shipment-notify-api\",\"url\":\"http://192.168.2.23/shipment-service/jhd-shipment-notify-api.git\"}, {\"projectname\":\"jhd-social-depot-api\",\"url\":\"http://192.168.2.23/shipment-service/social-depot-api.git\"}, {\"projectname\":\"jhd-staff-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-staff-api.git\"}, {\"projectname\":\"jhd-supplier-basic-api\",\"url\":\"http://192.168.2.23/member-service/jhd-supplier-basic-api.git\"}, {\"projectname\":\"jhd-sys-config-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/jhd-sys-config-api.git\"}, {\"projectname\":\"jhd-wechatpay-applyment-api\",\"url\":\"http://192.168.2.23/member-service/jhd-wechatpay-applyment-api.git\"}, {\"projectname\":\"jhd-wechatpay-applyment-basic-api\",\"url\":\"http://192.168.2.23/member-service/jhd-wechatpay-applyment-basic-api.git\"}, {\"projectname\":\"location-basic-api\",\"url\":\"http://192.168.2.23/member-service/location-basic-api.git\"}, {\"projectname\":\"merchant-gateway-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/merchant-gateway-api.git\"}, {\"projectname\":\"merchant-mall-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/merchant-mall-api.git\"}, {\"projectname\":\"o2o-mall-api\",\"url\":\"http://192.168.2.23/member-service/o2o-mall-api.git\"}, {\"projectname\":\"salesapp-web-api\",\"url\":\"http://192.168.2.23/merchant-mall-service/salesapp-web-api.git\"}, {\"projectname\":\"social-depot-gateway-api\",\"url\":\"http://192.168.2.23/shipment-service/social-depot-gateway-api.git\"}]";
        JSONArray projects = JSONObject.parseArray(project);
        List<String> projectNames = projects.stream().map(T -> {
            JSONObject jsonObject = JSONObject.parseObject(T.toString());
            return jsonObject.getString("projectname").trim();
        }).collect(Collectors.toList());
        System.out.println(JSONObject.toJSONString(projectNames));


        RestTemplate restTemplate = new RestTemplate();
        for (String projectName : projectNames) {
            String result = restTemplate.getForObject(getProjectInfo + projectName, String.class);
            JSONArray jsonArray = JSONObject.parseArray(result);
            for (Object o : jsonArray) {
                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                String name = jsonObject.getString("name");
                String id = jsonObject.getString("id");
                //createHook(id,name);
                createBranches(id,name,projectName);
            }
        }

    }


    private static void createHook(String id, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", Integer.valueOf(id));
        map.put("url", hookUrl + name);
        map.put("token", "50bf429c455658b8d80cc383d71bace4");
        map.put("push_events", true);
        HttpClient.postInputStream(createHook1 + id + createHook2, map);
    }


    private static void createBranches(String id, String name,String a) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", Integer.valueOf(id));
        map.put("branch", "point-develop");
        map.put("ref", "develop");
        try {
           ResponseEntity responseEntity = HttpClient.postInputStream(createBranch1 + id + createBranch2, map);
            if(!responseEntity.getBody().toString().contains("already exists")){
                System.out.println( a);
            }
        } catch (Exception e){
            System.out.println(e.getMessage());
        }

    }
}



